﻿// RasterRadiationDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "RasterRadiationDialog.h"
#include "afxdialogex.h"

#include <fstream>
#include "ckLandsat05.h"


// CRasterRadiationDialog 对话框
void WriteRadiation(ck::data::Landsat* pLandSat, ck::data::RasterRadiation& lsr, ck::data::RasterRadiation* bsr);

IMPLEMENT_DYNAMIC(CRasterRadiationDialog, CDialogEx)

CRasterRadiationDialog::CRasterRadiationDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_RASTER_RADIATION, pParent)
	, m_strPath(_T(""))
{
	//m_strPath = "F:\\Project\\项目实施\\TJ-数据质量检查-2018\\样例数据\\遥感影像对比数据\\数据\\参考影像\\19990910_1.tif";
	//m_strPath = "F:\\Project\\项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1";
	m_strPath = "E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1";
	m_pLandsat = NULL;
}

CRasterRadiationDialog::~CRasterRadiationDialog()
{
}

void CRasterRadiationDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_BAND, m_cmbBand);
	DDX_Text(pDX, IDC_EDIT_RASTER_PATH, m_strPath);
	DDX_Control(pDX, IDC_LIST_RADIATION, m_list);
	DDX_Control(pDX, IDC_PROGRESS1, m_progressbar);
}


BEGIN_MESSAGE_MAP(CRasterRadiationDialog, CDialogEx)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_SELECT_FILE, &CRasterRadiationDialog::OnBnClickedBtnSelectFile)
	ON_BN_CLICKED(IDOK, &CRasterRadiationDialog::OnBnClickedOk)
END_MESSAGE_MAP()


// CRasterRadiationDialog 消息处理程序


BOOL CRasterRadiationDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	UpdateData(FALSE);

	UpdateData(TRUE);
	ck::data::RasterFactory* pRasterFactory = ck::data::RasterFactory::GetInstance();
	RESULTCODE rc = RC_FAILURE;

	//m_pLandsat = pRasterFactory->CreateLandsat05();
	m_pLandsat = new ck::data::Landsat05();
	m_pLandsat->SetPath(m_strPath);
	rc = m_pLandsat->Open();
	if (rc != RC_SUCCESS)
	{
		return TRUE;
	}

	CString str;
	z_uint32 nBands = m_pLandsat->GetBandCount();
	for (z_uint32 i = 0; i < nBands; i++)
	{
		str.Format("%d", i);
		m_cmbBand.AddString(str);
	}
	m_cmbBand.SetCurSel(0);


	CRect rect;
	m_list.GetWindowRect(&rect);
	int width = rect.Width() * 0.99;

	m_list.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS);


	m_list.InsertColumn(0, "参数", LVCFMT_CENTER, width / 4);
	m_list.InsertColumn(1, "值", LVCFMT_CENTER, width * 3 / 4);

	m_list.InsertItem(m_list.GetItemCount(), "无效像元比");
	m_list.InsertItem(m_list.GetItemCount(), "信息量");
	m_list.InsertItem(m_list.GetItemCount(), "对比度");
	m_list.InsertItem(m_list.GetItemCount(), "均值");
	m_list.InsertItem(m_list.GetItemCount(), "信噪比");
	m_list.InsertItem(m_list.GetItemCount(), "清晰度");
	m_list.InsertItem(m_list.GetItemCount(), "偏斜");
	m_list.InsertItem(m_list.GetItemCount(), "陡度");

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CRasterRadiationDialog::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	if (m_pLandsat)
	{
		m_pLandsat->Release();
		m_pLandsat = NULL;
	}
}


void CRasterRadiationDialog::OnBnClickedBtnSelectFile()
{
	// TODO: 在此添加控件通知处理程序代码
	//const char*  szFilter = "Tif文件 (*.tif)|*.tif";
	//CString filePath("");
	//CFileDialog dlg(TRUE, "tif", NULL, OFN_HIDEREADONLY, szFilter);

	//int newLayerCunt = 0;
	//if (dlg.DoModal() == IDOK)
	//{
	//	m_strPath = dlg.GetPathName();
	//	UpdateData(FALSE);

	//	if (m_pRaster)
	//	{
	//		m_pRaster->Release();
	//		m_pRaster = NULL;
	//	}

	//	ck::data::RasterFactory* pRasterFactory = ck::data::RasterFactory::GetInstance();
	//	RESULTCODE rc = RC_FAILURE;

	//	m_pRaster = pRasterFactory->CreateRaster();
	//	m_pRaster->SetPath(m_strPath);
	//	rc = m_pRaster->Open();
	//	if (rc == RC_SUCCESS)
	//	{
	//		m_cmbBand.Clear();

	//		CString str;
	//		z_uint32 nBands = m_pRaster->GetBandCount();
	//		for (z_uint32 i = 0; i < nBands; i++)
	//		{
	//			str.Format("%d", i);
	//			m_cmbBand.AddString(str);
	//		}
	//		m_cmbBand.SetCurSel(0);
	//	}


	//}

}



void CRasterRadiationDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!m_pLandsat)
	{
		return;
	}

	CString strBand;
	m_cmbBand.GetWindowText(strBand);
	z_uint32 nBands = m_pLandsat->GetBandCount() - 1;

	ck::data::RasterRadiation lsr;
	ck::data::RasterRadiation* brs = new ck::data::RasterRadiation[nBands];
	ck::data::RasterRadiation* br = NULL;
	
	m_progressbar.SetRange(0, nBands);

	ck::data::Raster* pRaster = NULL;
	ck::data::RasterBand* pBand = NULL;
	for (z_uint32 i = 0; i < nBands; i++)
		//for (z_uint32 i = 4; i < 5; i++)
	{
		TRACE1("迭代：%d\n", i);

		pRaster = m_pLandsat->OpenRaster(i);
		if (pRaster == NULL)
		{
			continue;
		}
		pBand = pRaster->GetBand(0);
		if (pBand)
		{
			ck::processor::raster::RasterBandRadiationProcessor rrp;
			rrp.SetBand(pBand);
			rrp.Execute();
			br = rrp.GetData();

			brs[i] = *br;
			lsr += *br;
		}

		lsr /= nBands;

		pRaster->Release();

		m_progressbar.SetPos(i + 1);
	}
	WriteRadiation(m_pLandsat, lsr, brs);

	delete[] brs;

	//CDialogEx::OnOK();
}


//
//
//void CRasterRadiationDialog::OnBnClickedOk()
//{
//	// TODO: 在此添加控件通知处理程序代码
//	if (!m_pLandsat)
//	{
//		return;
//	}
//
//	CString strBand;
//	m_cmbBand.GetWindowText(strBand);
//	z_uint32 nBands = m_pLandsat->GetBandCount()-1;
//
//	ck::data::ck_raster_radiation rrr;
//	memset(&rrr, 0, sizeof(ck::data::ck_raster_radiation));
//
//	ck::data::ck_raster_radiation* rrs = NULL;
//	rrs = (ck::data::ck_raster_radiation*)malloc(nBands * sizeof(ck::data::ck_raster_radiation));
//	memset(rrs, 0, nBands * sizeof(ck::data::ck_raster_radiation));
//
//	m_progressbar.SetRange(0, nBands);
//
//	ck::data::Raster* pRaster = NULL;
//	ck::data::RasterBand* pBand = NULL;
//	for (z_uint32 i = 0; i < nBands; i++)
//	//for (z_uint32 i = 4; i < 5; i++)
//	{
//		TRACE1("迭代：%d\n", i);
//		m_progressbar.SetPos(i+1);
//
//		pRaster = m_pLandsat->OpenRaster(i);
//		if (pRaster == NULL)
//		{
//			continue;
//		}
//		pBand = pRaster->GetBand(0);
//		if (pBand)
//		{
//			ck::data::ck_raster_radiation* rr = rrs + i;		
//
//			pBand->ReadData();
//		
//			//无效像元比
//			ck::data::RasterMask* pMask = NULL;
//			pBand->CreateMask();
//			pMask = pBand->GetMask();
//			rr->ndr = pMask->GetNoDataRadio();
//			//信息量
//			rr->entropy = pBand->GetEntropy();
//			//对比度
//			rr->constrast = pBand->GetContrast();
//			//均值，信噪比
//			pBand->GetSNR(rr->mean, rr->snr);
//			//清晰度
//			rr->sharpness = pBand->GetSharpnenss();
//	
//			if (!pBand->HasHistogram())
//			{
//				pBand->ComputeHistogram();
//			}
//			ck::data::RasterHistogram* pHistogram = pBand->GetHistogram();
//			pHistogram->ComputeProbability();
//			pHistogram->ComputeStatistics();
//			//偏斜
//			rr->s = pHistogram->ComputeS();
//			//陡度
//			rr->k = pHistogram->ComputeK();
//
//			//========================================================================
//			
//			
//			//========================================================================
//			//计算均值
//			rrr.ndr += rr->ndr / nBands;
//			rrr.entropy += rr->entropy / nBands;
//			rrr.constrast += rr->constrast / nBands;
//			rrr.mean += rr->mean / nBands;
//			rrr.snr += rr->snr / nBands;
//			rrr.sharpness += rr->sharpness / nBands;
//			rrr.s += rr->s / nBands;
//			rrr.k += rr->k / nBands;
//
//			//========================================================================
//			pBand->ReleaseData();
//		}
//
//		pRaster->Release();
//	}
//	WriteRadiation(m_pLandsat, &rrr, rrs);
//
//	//CDialogEx::OnOK();
//}


//
//void CRasterRadiationDialog::OnBnClickedOk()
//{
//	// TODO: 在此添加控件通知处理程序代码
//	if (!m_pLandsat)
//	{
//		return;
//	}
//
//	CString strBand;
//	m_cmbBand.GetWindowText(strBand);
//	z_uint32 nBand = atoi(strBand);
//
//	DWORD ts = GetTickCount();
//
//	ck::data::RasterBand* pBand = m_pLandsat->GetBand(nBand);
//	if (!pBand)
//	{
//		AfxMessageBox("错误");
//		return;
//	}
//
//	ck::data::ck_raster_radiation rr;
//
//	//无效像元比
//	ck::data::RasterMask* pMask = NULL;
//	pBand->CreateMask();
//	pMask = pBand->GetMask();
//	rr.ndr = pMask->GetNoDataRadio();
//	//信息量
//	rr.entropy = pBand->GetEntropy();
//	//对比度
//	rr.constrast = pBand->GetContrast();
//	//均值，信噪比
//	pBand->GetSNR(rr.mean, rr.snr);
//	//清晰度
//	rr.sharpness = pBand->GetSharpnenss();
//	
//	if (!pBand->HasHistogram())
//	{
//		pBand->ComputeHistogram();
//	}
//	ck::data::RasterHistogram* pHistogram = pBand->GetHistogram();
//	pHistogram->ComputeProbability();
//	pHistogram->ComputeStatistics();
//	//偏斜
//	rr.s = pHistogram->ComputeS();
//	//陡度
//	rr.k = pHistogram->ComputeK();
//
//	//////////////////////////////////////////////////////////////////////////
//
//	//无效像元比
//	CString strText;
//	strText.Format("%f", rr.ndr);
//	m_list.SetItemText(1, 1, strText);
//	//信息量
//	strText.Format("%f", rr.entropy);
//	m_list.SetItemText(1, 1, strText);
//	//对比度
//	strText.Format("%f", rr.constrast);
//	m_list.SetItemText(2, 1, strText);
//	//均值
//	strText.Format("%f", rr.mean);
//	m_list.SetItemText(3, 1, strText);
//	//信噪比
//	strText.Format("%f", rr.snr);
//	m_list.SetItemText(4, 1, strText);
//	//清晰度
//	strText.Format("%f", rr.sharpness);
//	m_list.SetItemText(5, 1, strText);
//	//偏斜
//	strText.Format("%f", rr.s);
//	m_list.SetItemText(6, 1, strText);
//	//陡度
//	strText.Format("%f", rr.k);
//	m_list.SetItemText(7, 1, strText);
//
//	DWORD te = GetTickCount();
//	strText.Format("%f8.3秒", (te - ts) / 1000.0);
//	AfxMessageBox(strText);
//
//
//	//CDialogEx::OnOK();
//}

void WriteRadiation(ck::data::Landsat* pLandSat, ck::data::RasterRadiation& lsr, ck::data::RasterRadiation* bsr)
//void WriteRadiation(ck::data::Landsat* pLandSat, ck::data::ck_raster_radiation* rrr, ck::data::ck_raster_radiation* rrs)
{
	double cc = pLandSat->ComputeCloudCoverage();
	ck::data::ck_raster_radiation* rrr = lsr.GetData();
	ck::data::ck_raster_radiation* rrs = NULL;

	std::ofstream os("e:\\temp\\radiation.txt");

	os << "Landsat 5辐射参数" << std::endl;
	os << "产品名称:" << "\t" << pLandSat->GetName() << std::endl;
	os << "产品路径:" << "\t" << pLandSat->GetPath() << std::endl;
	os << std::endl;

	char text[Z_MESSAGE_MAX];
	os << "平均值:" << std::endl;
	os << "辐射参数:" << std::endl;	
	sprintf(text, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", "无效像元比", "信息量", "对比度","均值", "信噪比", "平均梯度", "偏斜", "陡度");
	os << text << std::endl;
	sprintf(text, "%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f", rrr->ndr, rrr->entropy, rrr->constrast, rrr->mean, rrr->snr, rrr->mean_gradient, rrr->s, rrr->k);
	os << text << std::endl;

	os << "灰度共生矩阵:" << std::endl;
	sprintf(text, "%s\t%s\t%s\t%s", "角二阶矩", "信息量", "对比度", "相关性");
	os << text << std::endl;
//	sprintf(text, "%8.3f\t%8.3f\t%8.3f\t%8.3f", rrr->glcm.gasm, rrr->glcm.entropy, rrr->glcm.constrast, rrr->glcm.coorelation);
	os << text << std::endl;
		

	const char* szBandKey= NULL;
	ck::data::RasterBand* pBand = NULL;
	z_uint32 nBands = pLandSat->GetBandCount()-1;

	os << std::endl;
	os << "波段辐射值:" << std::endl;
	sprintf(text, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s", "波段", "无效像元比", "信息量", "对比度", " 均值", "信噪比", "平均梯度", "偏斜", "陡度");
	os << text << std::endl;
	for (z_uint32 i = 0; i < nBands; i++)
	{
		szBandKey = pLandSat->GetBandKey(i);
		ck::data::ck_raster_radiation* rr = bsr[i].GetData();

		sprintf(text, "%s\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f\t%8.3f", szBandKey, rr->ndr, rr->entropy, rr->constrast, rr->mean, rr->snr, rr->mean_gradient, rr->s, rr->k);
		os << text << std::endl;
	}

	os << "灰度共生矩阵:" << std::endl;
	sprintf(text, "%s\t%s\t%s\t%s", "角二阶矩", "信息量", "对比度", "相关性");
	for (z_uint32 i = 0; i < nBands; i++)
	{
		szBandKey = pLandSat->GetBandKey(i);
		ck::data::ck_raster_radiation* rr = bsr[i].GetData();
		
		os << text << std::endl;
//		sprintf(text, "%8.3f\t%8.3f\t%8.3f\t%8.3f", rr->glcm.gasm, rr->glcm.entropy, rr->glcm.constrast, rr->glcm.coorelation);
		os << text << std::endl;
	}
	
	AfxMessageBox("计算完成");

	os.close();
}